package com.tim.app.ui.fragment.search;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.application.library.base.BaseFragment;
import com.application.library.log.DLOG;
import com.application.library.runtime.event.EventListener;
import com.application.library.runtime.event.EventManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.constant.EventTag;

/**
 * 问题搜索的父页面
 */
public class SearchProblemContainerFragment extends BaseFragment {

    private static final String TAG = "SearchProblemContainerFragment";
    private View rootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == rootView) {
            rootView = new FrameLayout(getContext());
            rootView.setId(R.id.flProblemSearchContainer);
            showRecommandView();
        }
        EventManager.ins().registListener(EventTag.DO_SEARCH, eventListener);
        EventManager.ins().registListener(EventTag.DO_SHOW_SEARCH_RECOMMAND, eventListener);
        return rootView;
    }


    EventListener eventListener = new EventListener() {
        @Override
        public void handleMessage(int what, int arg1, int arg2, Object dataobj) {
            switch (what) {
                case EventTag.DO_SEARCH:
                    showResultView(String.valueOf(dataobj));
                    break;
                case EventTag.DO_SHOW_SEARCH_RECOMMAND:
                    showRecommandView();
                    break;
            }
        }
    };

    /**
     * 显示推荐搜索的页面
     */
    private void showRecommandView() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.flProblemSearchContainer, new SearchRecommandFragment());
        transaction.commitAllowingStateLoss();
    }

    /**
     * 显示搜索结果
     */
    private void showResultView(String keyword) {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        SearchResultProblemFragment fragment = new SearchResultProblemFragment();
        Bundle bundle = new Bundle();
        bundle.putString(AppKey.SERACH_KEYWORD, keyword);
        fragment.setArguments(bundle);
        transaction.replace(R.id.flProblemSearchContainer, fragment);
        transaction.commit();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventManager.ins().removeListener(EventTag.DO_SEARCH, eventListener);
        EventManager.ins().removeListener(EventTag.DO_SHOW_SEARCH_RECOMMAND, eventListener);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(TAG);
    }


}
